package com.web58.velocity.controller;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.web58.velocity.bean.User;
import com.web58.velocity.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping
	@ResponseBody
	public String index() {
		return "Hello! Spring Boot~";
	}

	@RequestMapping(value = { "/user/{name}" }, method = { RequestMethod.GET })
	@ResponseBody
	public User findOne(@PathVariable("name") String name) throws Exception {
		Future<User> res = userService.findByName(name);
		return res.get(1000, TimeUnit.MILLISECONDS);
	}

	@RequestMapping(value = { "/user" }, method = { RequestMethod.GET })
	@ResponseBody
	public String register(@RequestBody User user) {
		userService.addUser(user);
		return "success";
	}

	@RequestMapping("/velocity")
	public String velocity(Model model) {
		model.addAttribute("user", new User(1, "zhaoshichao", "zsc234567", "5555@qq.com") );
		return "hello";
	}
}
